/**
 *  @(#)  IFAAAuthenticator  module    1.0  2017/03/24
 *
 *  Copyright  (c)  2017,  IFAA  and/or  its  affiliates.  All  rights  reserved.
 *
 *  Redistribution  and  use  in  source  and  binary  forms,  with  or  without
 *  modification,  are  permitted  provided  that  the  following  conditions  are  met:
 *
 *  -Redistribution  of  source  code  must  retain  the  above  copyright  notice,  this
 *    list  of  conditions  and  the  following  disclaimer.
 *
 *  -Redistribution  in  binary  form  must  reproduce  the  above  copyright  notice,
 *    this  list  of  conditions  and  the  following  disclaimer  in  the  documentation
 *    and/or  other  materials  provided  with  the  distribution.
 *
 *  Neither  the  name  of  IFAA  or  the  names  of  contributors  may
 *  be  used  to  endorse  or  promote  products  derived  from  this  software  without
 *  specific  prior  written  permission.
 *
 *  This  software  is  provided  "AS  IS,"  without  a  warranty  of  any  kind.  ALL
 *  EXPRESS  OR  IMPLIED  CONDITIONS,  REPRESENTATIONS  AND  WARRANTIES,  INCLUDING
 *  ANY  IMPLIED  WARRANTY  OF  MERCHANTABILITY,  FITNESS  FOR  A  PARTICULAR  PURPOSE
 *  OR  NON-INFRINGEMENT,  ARE  HEREBY  EXCLUDED.  IFAA,  ORG.
 *  AND  ITS  LICENSORS  SHALL  NOT  BE  LIABLE  FOR  ANY  DAMAGES  SUFFERED  BY  LICENSEE
 *  AS  A  RESULT  OF  USING,  MODIFYING  OR  DISTRIBUTING  THIS  SOFTWARE  OR  ITS
 *  DERIVATIVES.  IN  NO  EVENT  WILL  IFAA  OR  ITS  LICENSORS  BE  LIABLE  FOR  ANY  LOST
 *  REVENUE,  PROFIT  OR  DATA,  OR  FOR  DIRECT,  INDIRECT,  SPECIAL,  CONSEQUENTIAL,
 *  INCIDENTAL  OR  PUNITIVE  DAMAGES,  HOWEVER  CAUSED  AND  REGARDLESS  OF  THE  THEORY
 *  OF  LIABILITY,  ARISING  OUT  OF  THE  USE  OF  OR  INABILITY  TO  USE  THIS  SOFTWARE,
 *  EVEN  IF  IFAA  HAS  BEEN  ADVISED  OF  THE  POSSIBILITY  OF  SUCH  DAMAGES.
 *
 *  You  acknowledge  that  this  software  is  not  designed,  licensed  or  intended
 *  for  use  in  the  design,  construction,  operation  or  maintenance  of  any
 *  nuclear  facility.
 *
 */
package com.ifaa.sdk.authenticatorservice.fingerprint.flow;

import org.ifaa.ifaf.message.IFAFMessage;

import android.content.Context;
import android.os.Bundle;
import android.util.Base64;

import com.ifaa.sdk.auth.AuthenticatorCallback;
import com.ifaa.sdk.authenticatorservice.fingerprint.ta.TACommands;
import com.ifaa.sdk.authenticatorservice.fingerprint.ta.TAInterationV1;
import com.ifaa.sdk.authenticatorservice.fingerprint.util.FingerprintDataUtil;
import com.ifaa.sdk.authenticatorservice.message.Result;
import com.ifaa.sdk.auth.AuthenticatorLOG;
import com.ifaa.sdk.auth.message.AuthenticatorMessage;
import com.ifaa.sdk.auth.message.AuthenticatorResponse;
import com.google.gson.Gson;

/**
 * 
 * @author qiyi.wxc
 * @version 1.0
 */
public class FingerprintDeregister extends FingerprintAuth {

    protected final static String TAG = FingerprintAuthenticate.class.getSimpleName();

    /**
     * @param context
     * @param message
     */
    public FingerprintDeregister(Context context, Bundle message, AuthenticatorCallback callback) {
        super(context, message, callback);
    }

    @Override
    protected Bundle doTransaction() {

        try {
            String message = mMessage.getString(AuthenticatorMessage.KEY_MESSAGE);
            IFAFMessage authenticationRequest = new Gson().fromJson(message, IFAFMessage.class);

            byte[] data = Base64.decode(authenticationRequest.getSignedData().getIdentifyData(),
                Base64.URL_SAFE);
            long tmCost = System.currentTimeMillis();
            Result result = TAInterationV1.sendCommandAndData(mContext,
                TACommands.COMMAND_FINGERPRINT_DEREG, data);
            tmCost = System.currentTimeMillis() - tmCost;
            // if success
            if (result.getStatus() == 0) {
                return FingerprintDataUtil.constructResultBundle(getReponseType(),
                    AuthenticatorResponse.RESULT_SUCCESS);

            } else {
                AuthenticatorLOG.error(TAG, "fingerprint auth TA onResult not 0, "
                                            + Result.getStatusStringIFAA2(result.getStatus()));
                return FingerprintDataUtil.constructResultBundle(getReponseType(),
                    AuthenticatorResponse.RESULT_FAILURE, result.getStatus());
            }
        } catch (Throwable e) {
            AuthenticatorLOG.error(TAG, e);
            return FingerprintDataUtil.constructResultBundle(getReponseType(),
                AuthenticatorResponse.RESULT_FAILURE);
        }
    }

    @Override
    protected int getReponseType() {
        return AuthenticatorMessage.MSG_DEREGISTER_RESPONSE;
    }

}
